from fastapi import FastAPI, Request, Response


def register_middleware(app: FastAPI):
    """ 请求拦截与响应拦截 -- https://fastapi.tiangolo.com/tutorial/middleware/ """

    @app.middleware("http")
    async def intercept(request: Request, call_next) -> Response:
        """
            在这里获取 request.body() 出现了问题...
            使用了自定义路由APIRoute https://fastapi.tiangolo.com/advanced/custom-request-and-route/#custom-apiroute-class-in-a-router
            可以在这里对响应进行二次封装
        """
        response = await call_next(request)
        # 获取请求信息 request.headers.get('Authorization')

        # 比如这里给每个响应header添加字段
        # response.headers["X-Process-Time"] = 'test'
        return response
